﻿using System;
using System.Windows.Forms;

using AmazonBookLinker.Properties;


namespace AmazonBookLinker
{
  public partial class SettingsForm : Form
  {
    public SettingsForm()
    {
      InitializeComponent();
      PopulateMaxResultsInfo();
      PopulateLocaleInfo();
      PopulateImageSizes();
      RestoreCurrentUISettings();
    }

    private void RestoreCurrentUISettings()
    {
      Settings currentSettings = Settings.Default;

      AssociateTag = currentSettings.AssociateTag;
      AmazonLocale = currentSettings.AmazonLocale;
      MaxResults = currentSettings.MaxResults;
      DefaultImageSize = currentSettings.DefaultImageSize;
    }

    private void PopulateMaxResultsInfo()
    {
      cboMaxResults.Items.Clear();

      for ( int resultsCount = 10; resultsCount <= 50; resultsCount += 10 )
        cboMaxResults.Items.Add( resultsCount );

      cboMaxResults.SelectedIndex = 0;
    }

    private void PopulateImageSizes()
    {
      cboDefaultImageSizes.Items.Clear();
      cboDefaultImageSizes.Items.Add( BookImageSize.Small );
      cboDefaultImageSizes.Items.Add( BookImageSize.Medium );
      cboDefaultImageSizes.Items.Add( BookImageSize.Large );

      cboDefaultImageSizes.SelectedIndex = 0;
    }

    private void PopulateLocaleInfo()
    {
      cboLocales.Items.Clear();

      cboLocales.Items.Add( new AmazonComboBoxItem( "Amazon.com", "us.png" ) );
      cboLocales.Items.Add( new AmazonComboBoxItem( "Amazon.ca", "ca.png" ) );
      cboLocales.Items.Add( new AmazonComboBoxItem( "Amazon.co.uk", "uk.png" ) );
      cboLocales.Items.Add( new AmazonComboBoxItem( "Amazon.de", "de.png" ) );
      cboLocales.Items.Add( new AmazonComboBoxItem( "Amazon.fr", "fr.png" ) );
      cboLocales.Items.Add( new AmazonComboBoxItem( "Amazon.jp", "jp.png" ) );

      cboLocales.SelectedIndex = 0;
    }

    private void btnCancel_Click( object sender, EventArgs e )
    {
      DialogResult = DialogResult.Cancel;
      Close();
    }

    private void btnSave_Click( object sender, EventArgs e )
    {
      SaveSettings();
      DialogResult = MessageBox.Show( "Your settings were successfully saved. You must perform a new search for the changes to take effect.", "Save Settings", MessageBoxButtons.OK, MessageBoxIcon.Information );
      Close();
    }

    private void SaveSettings()
    {
      Settings.Default.AssociateTag = (string.IsNullOrEmpty( AssociateTag ) ? Settings.Default.DefaultAssociateTag : AssociateTag);
      Settings.Default.AmazonLocale = AmazonLocale;
      Settings.Default.MaxResults = MaxResults;
      Settings.Default.DefaultImageSize = DefaultImageSize;
      Settings.Default.Save();
    }

    private string AmazonLocale
    {
      get { return cboLocales.SelectedItem.ToString(); }
      set
      {
        int totalLocales = cboLocales.Items.Count;

        for ( int itemIndex = 0; itemIndex < totalLocales; itemIndex++ )
        {
          if ( ((AmazonComboBoxItem)cboLocales.Items[itemIndex]).Host == value )
          {
            cboLocales.SelectedIndex = itemIndex;
            break;
          }
        }
      }
    }

    private string DefaultImageSize
    {
      get
      {
        return this.cboDefaultImageSizes.SelectedItem.ToString();
      }
      set
      {
        int count = cboDefaultImageSizes.Items.Count;

        for (int index = 0; index < count; index++)
        {
          if (cboDefaultImageSizes.Items[index].ToString() == value)
          {
            cboDefaultImageSizes.SelectedIndex = index;
            break;
          }
        }
      }
    }

    private int MaxResults
    {
      get { return int.Parse( cboMaxResults.Text ); }
      set
      {
        int totalMaxResults = cboMaxResults.Items.Count;

        for ( int itemIndex = 0; itemIndex < totalMaxResults; itemIndex++ )
        {
          int itemValue = int.Parse( cboMaxResults.Items[itemIndex].ToString() );
          if ( itemValue == value )
          {
            cboMaxResults.SelectedIndex = itemIndex;
            break;
          }
        }
      }
    }

    private string AssociateTag
    {
      get
      {
        string userAssociateTag = txtAssociateTag.Text.Trim();

        if ( string.IsNullOrEmpty( userAssociateTag ) )
          return Settings.Default.DefaultAssociateTag;

        return userAssociateTag;
      }
      set { txtAssociateTag.Text = value; }
    }

    private class AmazonComboBoxItem
    {
      private readonly string flagFilename;
      private readonly string host;

      public AmazonComboBoxItem( string domain, string flagFilename )
      {
        host = domain;
        this.flagFilename = flagFilename;
      }

      public string Host
      {
        get { return host; }
      }

      public string FlagFilename
      {
        get { return flagFilename; }
      }

      public override string ToString()
      {
        return host;
      }
    }

    private void cboLocales_SelectedIndexChanged( object sender, EventArgs e )
    {
      // Display corresponding flag based on locale
      string filename = ((AmazonComboBoxItem)cboLocales.SelectedItem).FlagFilename;
      picCountryFlag.Image = PluginHelper.GetEmbeddedImage( filename );
    }
  }
}